package com.xindaoapp.happypet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.bean.UserInfo;
import com.xindaoapp.happypet.fragment.OhterTab1Fragment;
import com.xindaoapp.happypet.fragment.OhterTab2Fragment;
import com.xindaoapp.happypet.fragment.OhterTab3Fragment;
import com.xindaoapp.happypet.fragment.UIManager;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.ProgressHUD;
import com.xindaoapp.happypet.view.CircleImageView;

public class OtherActivity extends BaseActivity implements OnClickListener {

    private RelativeLayout tv_other_tab1;
    private RelativeLayout tv_other_tab2;
    private RelativeLayout tv_other_tab3;
    private RelativeLayout[] tabs;
    private TextView tv_name;
    private TextView fans_count;
    private CircleImageView persion_icon;
    private ImageView back;
    private ImageView iv_guanzhu;
    private ImageView sixin;

    private ImageView iv_tab1;
    private ImageView iv_tab2;
    private ImageView iv_tab3;


    private int index = 0;

    private String uid;
    private String name;
    private String face;

    private OhterTab1Fragment tab1Fragment;
    private OhterTab2Fragment tab2Fragment;
    private OhterTab3Fragment tab3Fragment;

    private Fragment currentF;

    private FragmentActivity mContext;

    private int fragmentLayout;
    private Bundle b;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_other);

        mContext = this;

        uid = getIntent().getStringExtra("uid");
        name = getIntent().getStringExtra("name");
        face = getIntent().getStringExtra("face");

        b = new Bundle();
        b.putString("uid", uid);

        initWidget();
        setSelect();
        initUserInfo(uid);
        setTabTitle(name);
    }

    private void initUserInfo(String uid2) {
        RequestParams params = new RequestParams();
        params.put("uid", uid2);
        // final ProgressHUD progressHUD = ProgressHUD.show(mContext, "正在加载...", true, true, null);
        AddClient.get(Address.GETUSERINFOBYUID, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                // if (null != progressHUD && progressHUD.isShowing()) {
                // progressHUD.dismiss();
                // }
                try {
                    UserInfo parse = JSON.parseObject(content, UserInfo.class);
                    follow = parse.userinfo.follow;
                    if (parse.getResult().equals("0")) {
                        tv_name.setText(parse.getUserinfo().username);
                        fans_count.setText(parse.userinfo.follow + "粉丝");
                        if (parse.userinfo.follow == 0) {
                            iv_guanzhu.setImageResource(R.drawable.other_add);
                        }
                        if (parse.userinfo.follow == 1) {
                            iv_guanzhu.setImageResource(R.drawable.other_app);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                // if (null != progressHUD && progressHUD.isShowing()) {
                // progressHUD.dismiss();
                // }
                super.onFailure(error, content);
            }
        });
    }

    private void initWidget() {

        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_other_tab1 = (RelativeLayout) findViewById(R.id.other_tab1);
        tv_other_tab2 = (RelativeLayout) findViewById(R.id.other_tab2);
        tv_other_tab3 = (RelativeLayout) findViewById(R.id.other_tab3);
        fans_count = (TextView) findViewById(R.id.fans_count);
        persion_icon = (CircleImageView) findViewById(R.id.persion_icon);
        back = (ImageView) findViewById(R.id.back);
        sixin = (ImageView) findViewById(R.id.sixin);
        iv_guanzhu = (ImageView) findViewById(R.id.iv_guanzhu);
        iv_tab1 = (ImageView) findViewById(R.id.iv_tab1);
        iv_tab1.setBackgroundResource(R.drawable.other_mylove_press);
        iv_tab2 = (ImageView) findViewById(R.id.iv_tab2);
        iv_tab3 = (ImageView) findViewById(R.id.iv_tab3);
        iv_guanzhu.setOnClickListener(this);

        tv_name.setText(name);
        ImageLoader.getInstance().displayImage(face, persion_icon);

        sixin.setOnClickListener(this);
        tv_other_tab1.setOnClickListener(this);
        tv_other_tab2.setOnClickListener(this);
        tv_other_tab3.setOnClickListener(this);
        back.setOnClickListener(this);

        tabs = new RelativeLayout[] {tv_other_tab1, tv_other_tab2, tv_other_tab3};

        tab1Fragment = OhterTab1Fragment.Instance(b);
        tab2Fragment = OhterTab2Fragment.Instance(b);
        tab3Fragment = OhterTab3Fragment.Instance(b);

        currentF = tab1Fragment;

        fragmentLayout = R.id.fragment;

        UIManager.getInstance().addFragment(mContext, tab1Fragment, false, null, fragmentLayout);

    }

    private int follow;

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.other_tab1:
                iv_tab1.setBackgroundResource(R.drawable.other_mylove_press);
                iv_tab2.setBackgroundResource(R.drawable.other_tiezi);
                iv_tab3.setBackgroundResource(R.drawable.other_guanzhu1);
                if (index != 0) {
                    index = 0;
                    setSelect();
                    UIManager.getInstance().switchContent(mContext, currentF, tab1Fragment,
                            fragmentLayout);
                    currentF = tab1Fragment;
                }

                break;
            case R.id.other_tab2:
                iv_tab1.setBackgroundResource(R.drawable.other_mylove);
                iv_tab2.setBackgroundResource(R.drawable.other_tiezi_press);
                iv_tab3.setBackgroundResource(R.drawable.other_guanzhu1);
                if (index != 1) {
                    index = 1;
                    setSelect();
                    UIManager.getInstance().switchContent(mContext, currentF, tab2Fragment,
                            fragmentLayout);
                    currentF = tab2Fragment;
                }
                break;
            case R.id.other_tab3:
                iv_tab1.setBackgroundResource(R.drawable.other_mylove);
                iv_tab2.setBackgroundResource(R.drawable.other_tiezi);
                iv_tab3.setBackgroundResource(R.drawable.other_guanzhu1_press);
                if (index != 2) {
                    index = 2;
                    setSelect();
                    UIManager.getInstance().switchContent(mContext, currentF, tab3Fragment,
                            fragmentLayout);
                    currentF = tab3Fragment;
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.iv_guanzhu:
                if (UserState.isLogged(mContext)) {
                    if ("0".equals(follow)) {
                        addGuanZhu(uid, name);
                    }
                    if ("1".equals(follow)) {
                        deleteGuanZhu(uid, 2);
                    }
                }
                break;
            case R.id.sixin:
                if (UserState.isLogged(mContext)) {
                    Intent privateMessage =
                            new Intent(OtherActivity.this, PrivateLetterActivity.class);
                    privateMessage.putExtra(Constants.KEY_TOUID, uid);
                    privateMessage.putExtra(Constants.KEY_USERNAME, name);
                    startActivity(privateMessage);
                }
                break;
        }
    }

    private void addGuanZhu(String uid2, String name2) {
        RequestParams params = new RequestParams();
        params.put("follow_uid", uid2);
        params.put("follow_username", name2);
        final ProgressHUD progressHUD = ProgressHUD.show(mContext, "正在加载...", true, true, null);
        AddClient.get(Address.GUANZHUOTHER, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                if (null != progressHUD && progressHUD.isShowing()) {
                    progressHUD.dismiss();
                }
                try {
                    BaseEntity parse = JSON.parseObject(content, BaseEntity.class);
                    if (parse.getResult().equals("0")) {
                        follow = 1;
                        Toast.makeText(OtherActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                        iv_guanzhu.setImageResource(R.drawable.other_app);
                    } else {
                        Toast.makeText(OtherActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(OtherActivity.this, "已关注", Toast.LENGTH_SHORT).show();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                if (null != progressHUD && progressHUD.isShowing()) {
                    progressHUD.dismiss();
                }
                super.onFailure(error, content);
            }
        });
    }

    private void deleteGuanZhu(String uid2, final int positon) {
        RequestParams params = new RequestParams();
        params.put("follow_uid", uid2);
        AddClient.get(Address.GUZNZHUDELETE, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                try {
                    follow = 0;
                    iv_guanzhu.setImageResource(R.drawable.other_add);
                    Toast.makeText(getApplicationContext(), "取消关注成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                Toast.makeText(getApplicationContext(), "取消关注失败", Toast.LENGTH_SHORT).show();
                super.onFailure(error, content);
            }
        });
    }

    private void setSelect() {
        for (int i = 0; i < tabs.length; i++) {
            if (i == index) {
                tabs[i].setSelected(true);
            } else {
                tabs[i].setSelected(false);
            }
        }
    }

}
